package com.rongxue.service.Impl;

import com.rongxue.mapper.SMobileCodeMapper;
import com.rongxue.pojo.MobileCode;
import com.rongxue.pojo.ObjectResult;
import com.rongxue.pojo.XMLBean;
import com.rongxue.service.MobileCodeService;
import com.rongxue.utils.CommonUtils;
import com.rongxue.utils.DateUtil;
import com.rongxue.utils.HttpClientUtil;
import com.rongxue.utils.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by yangweijian on 2018/1/15.
 */
@Service
@Transactional
public class MobileCodeServiceImpl implements MobileCodeService{

    public static final String SMS_KEY = "67a74ac326a63820d1231684b3bbe637";

    //短信请求URL
    public static final String SMS_URL = "http://v.juhe.cn/sms/send";

    @Autowired
    private SMobileCodeMapper sMobileCodeMapper;
    @Override
    public MobileCode selectCodeByMobile(String mobile) {
        return sMobileCodeMapper.selectCodeByMobile(mobile);
    }

    @Override
    public void insertMobileCode(Map<String, Object> map) {
        sMobileCodeMapper.insertMobileCode(map);
    }

    @Override
    public ObjectResult sms_code(XMLBean xmlBean, String ip) throws Exception {
        Map<String,Object> map = new HashMap<String,Object>();
        String code = RandomUtil.getRandom(6);

        String date1 = DateUtil.formatDate();
        String date2 = DateUtil.formatDate(DateUtil.addDateDay(new Date(), 1));
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        long startTime = format.parse(date1).getTime();
        long endTime = format.parse(date2).getTime();

        map.put("startTime",startTime);
        map.put("endTime",endTime);
        map.put("mobile",xmlBean.getMobile());
        Integer count = sMobileCodeMapper.selectMobileCodeCount(map);
        if(count>10){
            return  ObjectResult.build("0","该手机短信发送超过限制");
        }
        map.clear();
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        map.put("ip",ip);
        count = sMobileCodeMapper.selectMobileCodeCount(map);
        if(count>20){
            return  ObjectResult.build("0","该ip短信发送超过限制");
        }
        //添加纪录
        map.clear();
        map.put("mobile",xmlBean.getMobile());
        map.put("code",code);
        map.put("send_time",System.currentTimeMillis());
        map.put("ip",ip);
        sMobileCodeMapper.insertMobileCode(map);
        //发送短信验证码
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("mobile", xmlBean.getMobile());
        hashMap.put("tpl_id", "119785");
        hashMap.put("key", SMS_KEY);
        hashMap.put("tpl_value", "#code#="+code);
        ObjectResult objectResult2 = HttpClientUtil.verificationOfBankCard(SMS_URL, hashMap);
        return  ObjectResult.build("1","成功");
    }
}
